package top.scsoul.voes.web.controller.system;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.scsoul.voes.common.core.domain.AjaxResult;
import top.scsoul.voes.common.core.domain.entity.Permission;
import top.scsoul.voes.common.core.domain.model.IdsVo;
import top.scsoul.voes.system.service.PermissionService;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api/permission")
public class PermissionController {

    @Autowired
    PermissionService permissionService;

    @RequestMapping("/permissionDict")
    public AjaxResult permissionDict() {
        List<Permission> permissions = permissionService.queryAll(null);
        return AjaxResult.success(permissions);
    }

    @RequestMapping("/permissions")
    public AjaxResult permissions(
            @RequestParam(required = false) String permissionDesc,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageInfo<Permission> permissionPageInfo = permissionService.queryAll(permissionDesc, currentPage, pageSize);
        return AjaxResult.success(permissionPageInfo);
    }

    @RequestMapping("/get/{id}")
    public AjaxResult getPermissionById(@PathVariable int id) {
        Permission permission = permissionService.queryById(id);
        return AjaxResult.success(permission);
    }

    @RequestMapping("/add")
    public AjaxResult add(@Valid Permission permission) {
        permissionService.insert(permission);
        return AjaxResult.success(permission);
    }

    @RequestMapping("/delete")
    public AjaxResult delete(Integer id) {
        return AjaxResult.success(permissionService.deleteById(id));
    }

    @RequestMapping("/update")
    public AjaxResult update(@Valid Permission permission) {
        Permission updatePermission = permissionService.update(permission);
        return AjaxResult.success(updatePermission);
    }


    @RequestMapping("/export")
    public void export(@Valid IdsVo idsVo, HttpServletResponse response) throws IOException {
        permissionService.exportRole(idsVo.getIds(), response);
    }
}
